from pyecharts.charts import Bar
import pyecharts.options as opts
import pandas as pd
from pyecharts.charts import Line, Page, Grid
from ..models import *
import pyecharts.options as opts


# TODO 在本模块内封装绘图函数
def plot(city):
    data = DayData.objects.filter(city=city).values()
    data = list(data)
    data = pd.DataFrame(data, columns=['id', 'city', 'hightest_tem', 'lowest_tem', 'weather', 'date', 'wind_direction',
                                       'wind_level', 'weekday'])
    data.sort_values('date', inplace=True)
    date, max_tem, min_tem = data['date'].values.tolist(), data['hightest_tem'].values.tolist(), data[
        'lowest_tem'].values.tolist()
    bg_color = {
        'type': 'linear',
        'x1': 0,
        'y1': 0,
        'x2': 0,
        'y2': 1,
        'colorStops': [{
            'offset': 0,
            'color': 'darkviolet',
        }, {
            'offset': 1,
            'color': 'cyan'
        }]
    }
    max_color = {
        'type': 'linear',
        'x1': 0,
        'y1': 0,
        'x2': 0,
        'y2': 1,
        'colorStops': [{
            'offset': 0,
            'color': 'red',
        }, {
            'offset': 0.5,
            'color': 'orange'
        }, {
            'offset': 1,
            'color': 'yellow'
        }]
    }
    min_color = {
        'type': 'linear',
        'x1': 0,
        'y1': 0,
        'x2': 0,
        'y2': 1,
        'colorStops': [{
            'offset': 0,
            'color': 'violet',
        }, {
            'offset': 0.5,
            'color': 'green'
        }, {
            'offset': 1,
            'color': 'blue'
        }]
    }
    line = (
        Line(init_opts=opts.InitOpts(width='100%', height='800px', bg_color=bg_color, page_title=city + '温度分析'))
            .add_xaxis(date)
            .add_yaxis('最高温度', max_tem, color='white', is_smooth=True,
                       areastyle_opts=opts.AreaStyleOpts(opacity=0.5, color=max_color),
                       markpoint_opts=opts.MarkPointOpts(symbol='rect', symbol_size=8, label_opts=opts.LabelOpts(
                           color='lightpink', border_color='coral', border_width=3
                       )),
                       label_opts=opts.LabelOpts(
                           is_show=True, color='white', position='top'
                       ))
            .add_yaxis('最低温度', min_tem, color='cyan', is_smooth=True,
                       areastyle_opts=opts.AreaStyleOpts(opacity=0.5, color=min_color),
                       markpoint_opts=opts.MarkPointOpts(symbol='x', symbol_size=8, label_opts=opts.LabelOpts(
                           color='dodgerblue', border_color='darkviolet', border_width=3
                       )),
                       label_opts=opts.LabelOpts(
                           is_show=True, color='cyan', position='bottom'
                       ))
            .set_series_opts(linestyle_opts=opts.LineStyleOpts(width=1.5))
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title=city + '2011-2022年的最高温和最低温',
                pos_left='center',
                pos_top='10px',
                title_textstyle_opts=opts.TextStyleOpts(
                    font_size=20,
                    font_weight='bold',
                    color='white'
                )
            ),
            legend_opts=opts.LegendOpts(
                pos_top='7%',
                orient='vertical',
                legend_icon='rect',
                textstyle_opts=opts.TextStyleOpts(
                    color='white'
                )
            ),
            xaxis_opts=opts.AxisOpts(
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(color='white')
                )
            ),
            yaxis_opts=opts.AxisOpts(
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(color='white')
                )
            ),
            tooltip_opts=opts.TooltipOpts(trigger='axis'),
            datazoom_opts=opts.DataZoomOpts(type_='slider', range_start=0, range_end=5)
        )
    )
    data = MonthData.objects.filter(city=city)
    data = list(data.values())
    data = pd.DataFrame(data, columns=['city','month','avg_high_tem','avg_low_tem','extreme_high_tem','extreme_low_tem','avg_air_quality','best_air','best_air_date','worst_air','worst_air_date'])
    max_month = data.sort_values('avg_high_tem', ascending=False)[:10]
    month, max_month_tem = max_month['month'].values.tolist(), max_month['avg_high_tem'].values.tolist()
    color = {
        'type': 'linear',
        'x': 0,
        'y': 1,
        'x2': 1,
        'y2': 0,
        'colorStops': [{
            'offset': 0,
            'color': '#ceee16',
        }, {
            'offset': 0.5,
            'color': '#86cd01',
        }, {
            'offset': 1,
            'color': '#2a8c04',
        }]
    }
    bar1 = (
        Bar(init_opts=opts.InitOpts(width='900px', height='500px'))
        .add_xaxis(month)
        .add_yaxis('最热月份', max_month_tem, bar_width='50%', color=color,
                   itemstyle_opts=opts.ItemStyleOpts(
                       border_color='darkviolet',
                       border_width=1
                   ))
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title=city+'TOP10高温月份',
                pos_right='center',
                pos_top='1%',
                title_textstyle_opts=opts.TextStyleOpts(
                    color='white',
                    font_weight='bold'
                )
            ), legend_opts=opts.LegendOpts(
                is_show=False
            )
        )
    )
    min_month = data.sort_values('avg_low_tem')[:10]
    month, min_month_tem = min_month['month'].values.tolist(), min_month['avg_low_tem'].values.tolist()
    color = {
        'type': 'linear',
        'x': 0,
        'y': 1,
        'x2': 1,
        'y2': 0,
        'colorStops': [{
            'offset': 0,
            'color': '#7cecfc',
        }, {
            'offset': 0.5,
            'color': '#8b517f',
        }, {
            'offset': 1,
            'color': '#ed3f14',
        }]
    }
    bar2 = (
        Bar(init_opts=opts.InitOpts(width='900px', height='500px'))
            .add_xaxis(month)
            .add_yaxis('最冷月份', min_month_tem, bar_width='50%',
                       label_opts=opts.LabelOpts(
                           color='black'
                       ),
                       itemstyle_opts=opts.ItemStyleOpts(
                        border_color='#ff7600',
                        border_width=1,
                        color=color,
                   ))
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title=city + 'TOP10低温月份',
                pos_right='center',
                pos_top='95%',
                title_textstyle_opts=opts.TextStyleOpts(
                    color='white',
                    font_weight='bold'
                )
            ),
            legend_opts=opts.LegendOpts(
                is_show=False
            )
        )
    )
    data = DayData.objects.filter(city=city)
    data = list(data.values())
    data = pd.DataFrame(data, columns=['id','city','hightest_tem','lowest_tem','weather','date','wind_direction','wind_level','weekday' ])
    max_day = data.sort_values('hightest_tem', ascending=False)[:10]
    day, max_day_tem = max_day['date'].values.tolist(), max_day['hightest_tem'].values.tolist()
    color = {
        'type': 'linear',
        'x': 0,
        'y': 1,
        'x2': 1,
        'y2': 0,
        'colorStops': [{
            'offset': 0,
            'color': '#bd41e1',
        }, {
            'offset': 0.5,
            'color': '#4721a8',
        }, {
            'offset': 1,
            'color': '#1e88d1',
        }]
    }
    bar3 = (
        Bar(init_opts=opts.InitOpts(width='900px', height='500px'))
            .add_xaxis(day)
            .add_yaxis('最热日期', max_day_tem, bar_width='50%', color=color,
                       label_opts=opts.LabelOpts(
                           color='black'
                       ),
                       itemstyle_opts=opts.ItemStyleOpts(
                           border_color='#fccafd',
                           border_width=1
                       ))
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title=city + 'TOP10高温日期',
                pos_right='center',
                pos_top='1%',
                title_textstyle_opts=opts.TextStyleOpts(
                    color='white',
                    font_weight='bold'
                )
            ),
            legend_opts=opts.LegendOpts(
                is_show=False
            )
        )
    )
    min_day = data.sort_values('lowest_tem')[:10]
    day, min_day_tem = min_day['date'].values.tolist(), min_day['lowest_tem'].values.tolist()
    color = {
        'type': 'linear',
        'x': 0,
        'y': 1,
        'x2': 1,
        'y2': 0,
        'colorStops': [{
            'offset': 0,
            'color': '#fdfee7',
        }, {
            'offset': 0.5,
            'color': '#ea15e4',
        }, {
            'offset': 1,
            'color': '#bb160c',
        }]
    }
    bar4 = (
        Bar(init_opts=opts.InitOpts(width='900px', height='500px'))
            .add_xaxis(day)
            .add_yaxis('最冷日期', min_day_tem, bar_width='50%',
                       label_opts=opts.LabelOpts(
                           color='black'
                       ),
                       itemstyle_opts=opts.ItemStyleOpts(
                           border_color='#ff7600',
                           border_width=1,
                           color=color,
                       ))
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title=city + 'TOP10低温日期',
                pos_right='center',
                pos_top='95%',
                title_textstyle_opts=opts.TextStyleOpts(
                    color='white',
                    font_weight='bold'
                )
            ),
            legend_opts=opts.LegendOpts(
                is_show=False
            )
        )
    )

    bg_color = {
        'type': 'linear',
        'x': 0,
        'y': 0,
        'x2': 0,
        'y2': 1,
        'colorStops': [{
            'offset': 0,
            'color': 'darkviolet',
        }, {
            'offset': 1,
            'color': 'cyan',
        }]
    }
    gird1 = (
        Grid(init_opts=opts.InitOpts(width='50%', height='600px', bg_color=bg_color))
        .add(bar1, grid_opts=opts.GridOpts(
            pos_bottom='55%'
        ))
        .add(bar2, grid_opts=opts.GridOpts(
            pos_top='55%'
        ))
    )
    gird2 = (
        Grid(init_opts=opts.InitOpts(width='50%', height='600px', bg_color=bg_color))
            .add(bar3, grid_opts=opts.GridOpts(
                pos_bottom='55%'
        ))
            .add(bar4, grid_opts=opts.GridOpts(
                pos_top='55%'
        ))
    )
    page = (
        Page('{} 温度分析'.format(city), layout=Page.SimplePageLayout)
        .add(line)
        .add(gird1)
        .add(gird2)
    )
    return page
